package com.huitone.gddw.service.impl.monitoring.sms;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.entity.DicDef;
import com.huitone.gddw.dao.entity.PriRuleDef;
import com.huitone.gddw.dao.monitoring.sms.IPriQueueDao;
import com.huitone.gddw.service.IPriRuleDefService;
import com.huitone.gddw.service.monitoring.sms.IPriQueueService;
import com.huitone.gddw.service.system.IDicDefService;

/**
 * 优先级队列监控 接口实现类
 * @author wuluodan
 *
 */
@Service
public class PriQueueService implements IPriQueueService {

	private static final String OBJ_TYPE = "objType";
	private static final String FLOW = "flow";
	private static final String KEY = "pri";
	
	@Autowired
	private IPriQueueDao priQueueDao;
	
	@Autowired
	private IDicDefService dicDefService;
	
	@Autowired
	private IPriRuleDefService priRuleDefService;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> find(Map<String, Object> params) {
		Map<String, Object> paramsDic = new HashMap<String, Object>();
		paramsDic.put("dicName", OBJ_TYPE);
		paramsDic.put("dicValue", FLOW);
		DicDef dic = dicDefService.findOne(paramsDic);
		
		params.put("objType", dic.getDicId());
		params.put("key", KEY);
		List<Map<String, Object>> result = priQueueDao.selectPage(params);
		
		List<Map<String, Object>> all = null;
		if (null == result || 0 == result.size()) {
			return all;
		}
		
		all = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> re : result) {
			boolean isSame = false;
			for (Map<String, Object> map : all) {
				if (map.get("gateLinkId").toString().equals(re.get("GATE_LINK_ID").toString())) {
					isSame = true;

					Map<String, Object> mapLevel = (Map<String, Object>)map.get("level");
					mapLevel.put(re.get("APPLICATIONS").toString(), re.get("COUNT"));

					break;
				}
			}
			if (!isSame) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("gateLinkId", re.get("GATE_LINK_ID"));
				map.put("itemName", re.get("ITEM_NAME"));
				map.put("sum", 0);
				
				// 将优先级放进level，顺序排序
				Map<String, Object> mapLevel = new TreeMap<String, Object>(new Comparator<String>() {
                    public int compare(String k1, String k2) {
                        return Integer.valueOf(k1) - Integer.valueOf(k2);
                    }
                });
				mapLevel.put(re.get("APPLICATIONS").toString(), re.get("COUNT"));
				map.put("level", mapLevel);

				all.add(map);
			}
		}
		
		// 补全优先级数据
		if (params.get("level") != null && !"".equals(params.get("level").toString())) {
			String lv = params.get("level").toString();
			for (Map<String, Object> map : all) {
				Map<String, Object> mapLevel = (Map<String, Object>)map.get("level");
				if (!mapLevel.containsKey(lv)) {
					mapLevel.put(lv, 0);
				}
			}
		} else {
			List<PriRuleDef> listPrd = priRuleDefService.getAll();
			for (Map<String, Object> map : all) {
				Map<String, Object> mapLevel = (Map<String, Object>)map.get("level");
				for (PriRuleDef priRuleDef : listPrd) {
					if (!mapLevel.containsKey(priRuleDef.getLevel().toString())) {
						mapLevel.put(priRuleDef.getLevel().toString(), 0);
					}
				}
			}
		}
		
		// 统计每个通道总数
		List<Map<String, Object>> listSum = priQueueDao.selectTotal(params);
		for (Map<String, Object> map : listSum) {
			for (Map<String, Object> mapAll : all) {
				if (map.get("GATE_LINK_ID").toString().equals(mapAll.get("gateLinkId").toString())) {
					mapAll.put("sum", map.get("SUM"));
					break;
				}
			}
		}
		
		// 将优先级的key-value形式转换为两个单独的list
		/*for (Map<String, Object> mapAll : all) {
			Map<String, Object> mapLevel = (Map<String, Object>)mapAll.get("level");
			Set<Entry<String, Object>> entrySet = mapLevel.entrySet();
			List<String> levelName = new ArrayList<String>();
			List<String> levelVal = new ArrayList<String>();
			int i = 0;
			for (Entry<String, Object> entry : entrySet) {
				levelName.add(i, entry.getKey());
				levelVal.add(i, entry.getValue().toString());
				i++;
			}
			mapAll.put("levelName", levelName);
			mapAll.put("levelVal", levelVal);
		}*/
		
		return all;
	}

}
